//
//  ShoppingOneTableViewCell.m
//  JewelryStore
//
//  Created by apple on 2016/12/20.
//  Copyright © 2016年 com. All rights reserved.
//

#import "ShoppingOneTableViewCell.h"

@interface ShoppingOneTableViewCell ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *priceBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleBottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *selectBtnWidth;
@property (weak, nonatomic) IBOutlet UIView *deleteView;
@property (weak, nonatomic) IBOutlet UILabel *shopFreeSend;  //已免运费
@property (weak, nonatomic) IBOutlet UIButton *shopFreeBtn;  //优惠卷
@property (weak, nonatomic) IBOutlet UIButton *selectTopBtn;  //选中按钮
@property (weak, nonatomic) IBOutlet UIButton *selectBottomBtn;  //选中按钮
@property (nonatomic, strong) MGSwipeButton *deleteBtn;
@property (nonatomic, strong) MGSwipeButton *favoriteBtn;
@end

@implementation ShoppingOneTableViewCell

- (void)setShopNum:(int)shopNum
{
    _shopNum = shopNum;
    
    [_shopSegment setTitle:[NSString stringWithFormat:@"%d", shopNum] forSegmentAtIndex:1];

}
- (void)setModel:(NSArray *)model
{
    _model = model;
    
    _shopTitle.text = @"李财神珠宝2016";
    
    _shopName.text = @"商品名称";
    
    _shopSizeDecription.text = @"A386";
    
    _shopPrice.text = @"￥1024";
    
    [_shopSegment setTitle:@"1" forSegmentAtIndex:1];
    
//    [shopImage sd_setImageWithURL:KImageNamed(@"") placeholderImage:KImageNamed(@"")];
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self sizeToScreenWidth];
    
    [self setupSegment];
    
    [self setupMoreBtn];
}

- (void)prepareForReuse
{
    [super prepareForReuse];
}
#pragma mark - setupUI

- (void)setupSegment
{
    [_shopSegment setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    _shopSegment.layer.borderWidth = 1;
    _shopSegment.layer.borderColor = [UIColor lightGrayColor].CGColor;
}

- (void)sizeToScreenWidth
{
    //间距适配
    _imageWidth.constant = AUTO(SCREEN_WIDTH/2.8);
    _priceBottom.constant = -AUTO(SCREEN_WIDTH/50);
    _titleBottom.constant = AUTO(SCREEN_WIDTH/50);
    _selectBtnWidth.constant = AUTO(SCREEN_WIDTH/100)*5;
    
    //字体适配
    _shopTitle.font = Font(17);
    _shopName.font = Font(17);
    _shopSize.font = Font(15);
    _shopSizeDecription.font = Font(15);
    _shopCategory.font = Font(15);
    _shopCategoryDecription.font = Font(15);
    _shopPrice.font = Font(17);
    _shopFreeSend.font = Font(15);
    _shopFreeBtn.titleLabel.font = Font(15);
    
}

- (void)setupMoreBtn
{
    self.rightButtons = @[self.deleteBtn, self.favoriteBtn];
    
//    self.rightSwipeSettings.topMargin = 53;
}
#pragma mark - private

//勾选
- (IBAction)selectClick:(id)sender
{
    
//    if (!_isSelect) {
//        [self selectAll:_isSelect];
//    }else
//    {
//        [self selectNone:_isSelect];
//    }
//    _isSelect = !_isSelect;
    [self.oneDelegate selectOrCancel:self];
}

//优惠券
- (IBAction)freeClick:(id)sender
{
    TT_Log(@"弹出框选优惠卷");
}

//加减
- (IBAction)segementClick:(id)sender
{
    UISegmentedControl *segment = sender;
    
    int resultNum;
    
    switch (segment.selectedSegmentIndex) {
        case 0:
        {
            _shopNum = [[segment titleForSegmentAtIndex:1] intValue];
            
            int value = _shopNum;
            
            int reslut = value-1 > 0 ? value-1 : 1;
            [segment setTitle:[NSString stringWithFormat:@"%d", reslut] forSegmentAtIndex:1];
            segment.selectedSegmentIndex = 1;
            
            resultNum = reslut;
            
            [self totalCallback:resultNum case:SubtractType];
        }
            break;
        case 1:
            
            break;
        case 2:
        {
            int value = [[segment titleForSegmentAtIndex:1] intValue];
            [segment setTitle:[NSString stringWithFormat:@"%d", value+1] forSegmentAtIndex:1];
            segment.selectedSegmentIndex = 1;
            
            resultNum = value+1;
            
            [self totalCallback:resultNum case:AddType];
        }
            
        default:
            break;
    }
    
    
}

- (void)totalCallback:(int)resultNum case:(int)type
{
    if (_shopPrice.text.length > 1 && ![_shopPrice.text isEqualToString:@""] && ![_shopPrice.text isKindOfClass:[NSNull class]]) {
        
        [self.oneDelegate calculateItemTotalPrice:self total:resultNum case:type];
    }

}
- (void)setupBtnSelect
{
    [_selectTopBtn setBackgroundImage:KImageNamed(@"s_select") forState:UIControlStateNormal];
    [_selectBottomBtn setBackgroundImage:KImageNamed(@"s_select") forState:UIControlStateNormal];
    
}

- (void)setupBtnNone
{
    [_selectTopBtn setBackgroundImage:KImageNamed(@"m_noselect") forState:UIControlStateNormal];
    [_selectBottomBtn setBackgroundImage:KImageNamed(@"m_noselect") forState:UIControlStateNormal];
}

#pragma mark - public


#pragma mark - lazy

- (MGSwipeButton *)deleteBtn
{
    if (!_deleteBtn) {
        
        _deleteBtn = [MGSwipeButton buttonWithTitle:@"删除" backgroundColor:[UIColor redColor]];
        
    }
    return _deleteBtn;
}

- (MGSwipeButton *)favoriteBtn
{
    if (!_favoriteBtn) {
        
        _favoriteBtn = [MGSwipeButton buttonWithTitle:@"移入收藏" backgroundColor:RGB(94, 94, 94)];
    }
    return _favoriteBtn;
}
@end
